package com.jiang.thread;

import com.jiang.bean.Song;
import com.jiang.mapper.LuceneDao;
import com.jiang.mapper.SongMapper;
import com.jiang.utils.DebugUtil;

import java.io.IOException;
import java.util.List;

public class MyCrawlerThread extends Thread {
    private List<Song> songList;
    private SongMapper songMapper;
    private LuceneDao luceneDao;

    public void setLuceneDao(LuceneDao luceneDao) {
        this.luceneDao = luceneDao;
    }

    public MyCrawlerThread(List<Song> songList) {
        this.songList = songList;
    }

    public void setSongMapper(SongMapper songMapper) {
        this.songMapper = songMapper;
    }

    public List<Song> getSongList() {
        return songList;
    }

    public void setSongList(List<Song> songList) {
        this.songList = songList;
    }

    @Override
    public void run() {
        DebugUtil.sout("线程开始");
        int a = 0;
        for (Song song : songList) {
            DebugUtil.sout(a++);
            String KWSongId = song.getUrl();
            Song result = songMapper.selectKWSong(KWSongId);
            if (result == null) {
                int i = songMapper.insertSong(song);
                //如果存入数据库成功则增加索引
                if (i > 0) {
                    try {
                        luceneDao.addIndex(song);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        DebugUtil.sout("线程结束");
    }
}
